<?php
/**
 * Performs install/uninstall methods for the Facebookposts Plugin
 *
 * @package    Ushahidi
 * @author     Deji Lana
 * @copyright  (c) 2010 TranformNaija Team
 * @license    http://www.transformnaija.net
 */
class Facebookposts_Install {
	
	/**
	 * Constructor to load the shared database library
	 */
	public function __construct()
	{
		$this->db =  new Database();
	}

	/**
	 * Creates the required columns for the Facebook Post Plugin
	 */
	public function run_install()
	{
		
		// ****************************************
		// DATABASE STUFF
		// Is the Facebookposts Service already installed?
		$exists = ORM::factory('service')
			->where('service_name', 'Facebookposts')
			->find();
			
		if ( ! $exists->loaded)
		{
			$service = ORM::factory('service');
			$service->service_name = "Facebookposts";
			$service->service_description = "Facebook posts";
			$service->save();
		}
		
		//setting up Facebookposts  as a schedule in scheduler...
		$exists = ORM::factory('scheduler')
			->where('scheduler_name', 'Facebookposts')
			->find();
			
		if ( ! $exists->loaded)
		{
			$scheduler = ORM::factory('scheduler');
			$scheduler->scheduler_name = "Facebookposts";
			$scheduler->scheduler_last = time();
			$scheduler->scheduler_weekday = -1;
			$scheduler->scheduler_day = -1;
			$scheduler->scheduler_hour = -1;
			$scheduler->scheduler_minute = -1;
			$scheduler->scheduler_controller = 'facebookposts';
			$scheduler->scheduler_active = 1;
			$scheduler->save();
		}
		
		
		$this->db->query("
			CREATE TABLE IF NOT EXISTS `".Kohana::config('database.default.table_prefix')."facebookposts_settings`
			(
				id INT UNSIGNED NOT NULL AUTO_INCREMENT,
				since_date VARCHAR(50),
				PRIMARY KEY (id)
			);
		");
		
		$exists = ORM::factory('facebookposts_settings')
			->where('id', '1')
			->find();
			
		if ( ! $exists->loaded)
		{
		
		
		$this->db->query("
				INSERT INTO `".Kohana::config('database.default.table_prefix')."facebookposts_settings` (`id`,`since_date`)
				VALUES
					(1,'');
			");
			
		}
		
		
	}

	/**
	 * Deletes the columns for the Facebookposts Plugin
	 */
	public function uninstall()
	{
		ORM::factory('service')
			->where('service_name', 'Facebookposts')
			->delete_all();
			
//		$this->db->query("
//			DROP TABLE ".Kohana::config('database.default.table_prefix')."ivr_prompt;
//			DROP TABLE ".Kohana::config('database.default.table_prefix')."ivr_message;
//			");
			
		$this->db->query("
			DROP TABLE ".Kohana::config('database.default.table_prefix')."facebookposts_settings;
			");
			
		
	}
	
	/**
	 * Delete the Directory Recursively
	 * @param   string   directory to be deleted
	 * @param   bool   empty the directory instead and leave it in place
	 * @return  bool	
	 */
	private function _del_dir($directory, $empty=FALSE)
	{
		if(substr($directory,-1) == '/')
		{
			$directory = substr($directory,0,-1);
		}
		if(!file_exists($directory) || !is_dir($directory))
		{
			return FALSE;
		}elseif(is_readable($directory))
		{
			$handle = opendir($directory);
			while (FALSE !== ($item = readdir($handle)))
			{
				if($item != '.' && $item != '..')
				{
					$path = $directory.'/'.$item;
					if(is_dir($path)) 
					{
						recursive_remove_directory($path);
					}else{
						unlink($path);
					}
				}
			}
			closedir($handle);
			if($empty == FALSE)
			{
				if(!rmdir($directory))
				{
					return FALSE;
				}
			}
		}
		return TRUE;
	}
}